package com.bei_en.common.util.baidu_tuisong.sample;

import com.baidu.yun.core.log.YunLogEvent;
import com.baidu.yun.core.log.YunLogHandler;
import com.baidu.yun.push.auth.PushKeyPair;
import com.baidu.yun.push.client.BaiduPushClient;
import com.baidu.yun.push.constants.BaiduPushConstants;
import com.baidu.yun.push.exception.PushClientException;
import com.baidu.yun.push.exception.PushServerException;
import com.baidu.yun.push.model.CreateTagRequest;
import com.baidu.yun.push.model.CreateTagResponse;

public class CreateTag {

	public static void main(String[] args) throws PushClientException,
			PushServerException {

		// 1. get apiKey and secretKey from developer console
		String apiKey = "vxbbZlo1pIEEpXH1s8tFZb0V";
		String secretKey = "TbUBA2ZdQoKKSYtAvQ9ik5yBiZtGhIPK";
		PushKeyPair pair = new PushKeyPair(apiKey, secretKey);

		// 2. build a BaidupushClient object to access released interfaces
		BaiduPushClient pushClient = new BaiduPushClient(pair,
				BaiduPushConstants.CHANNEL_REST_URL);

		// 3. register a YunLogHandler to get detail interacting information
		// in this request.
		pushClient.setChannelLogHandler(new YunLogHandler() {

			@Override
			public void onHandle(YunLogEvent event) {

				System.out.println(event.getMessage());
			}
		});

		try {
			// 4. specify request arguments
			CreateTagRequest request = new CreateTagRequest().addTagName("aaa")
					.addDeviceType(3);
			// 5. http request
			CreateTagResponse response = pushClient.createTag(request);
			System.out.println(String.format("tagName: %s, result: %d",
					response.getTagName(), response.getResult()));
		} catch (PushClientException e) {
			if (BaiduPushConstants.ERROROPTTYPE) {
				throw e;
			} else {
				e.printStackTrace();
			}
		} catch (PushServerException e) {
			if (BaiduPushConstants.ERROROPTTYPE) {
				throw e;
			} else {
				System.out.println(String.format(
						"requestId: %d, errorCode: %d, errorMessage: %s",
						e.getRequestId(), e.getErrorCode(), e.getErrorMsg()));
			}
		}
	}
}
